package com.jian.socks5.udp.handler;

import com.jian.socks5.common.Constants;
import com.jian.socks5.udp.codec.Socks5UDPRequestDecoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.EventExecutorGroup;

/***
 * 初始化channel
 * @author Jian
 * @date 2021.06.21
 */
public class Socks5UDPChannelInitializer extends ChannelInitializer<Channel> {

    private EventExecutorGroup eventExecutors;

    private static Socks5UDPChannelInitializer socks5UdpChannelInitializer;

    public static Socks5UDPChannelInitializer getSocks5UDPChannelInitializer(EventExecutorGroup eventExecutors){
        if (socks5UdpChannelInitializer == null) {
            socks5UdpChannelInitializer = new Socks5UDPChannelInitializer(eventExecutors);
        }
        return socks5UdpChannelInitializer;
    }

    public Socks5UDPChannelInitializer(EventExecutorGroup eventExecutors) {
        this.eventExecutors = eventExecutors;
    }

    @Override
    protected void initChannel(Channel ch){
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(eventExecutors,Constants.socks5UDPResponseEncoder);
        pipeline.addLast(eventExecutors,new Socks5UDPRequestDecoder());
        pipeline.addLast(eventExecutors,Constants.socks5UDPHandler);
    }
}








